#!/usr/bin/env bash
# DeepDive
# Usage: deepdive COMMAND [ARG]...
#
# To enable bash completion for deepdive commands, run:
# $ source $(deepdive whereis installed etc/deepdive_bash_completion.sh)
#
##
# Generated: 2015-06-19
set -eu

if ! [[ "${DEEPDIVE_SHELL:-}" -ef "$0" ]]; then
    Self=$(readlink -f "$0" 2>/dev/null || {
        # XXX readlink -f is only available in GNU coreutils
        cd $(dirname -- "$0")
        n=$(basename -- "$0")
        if [ -L "$n" ]; then
            L=$(readlink "$n")
            if [ x"$L" != x"${L#/}" ]; then
                echo "$L"; exit
            else
                cd "$(dirname -- "$L")"
                n=$(basename -- "$L")
            fi
        fi
        echo "$(pwd -P)/$n"
    })
    Here=$(dirname "$Self")

    # Keep a backup of some environment variables
    for v in PATH LANGUAGE LANG LC_ALL LC_COLLATE LC_CTYPE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME
    do  vbak=DEEPDIVE_ORIG_$v
          declare -p $v    &>/dev/null || continue
        ! declare -p $vbak &>/dev/null || continue
        export "$vbak=${!v}"
    done

    # Setup environment
    unset CDPATH
    export DEEPDIVE_HOME=${Here%/bin}

    #  ensure the user's language/locale setting have minimal impact on
    #  DeepDive's behavior (Ideally, something like C.UTF-8 should be used, but
    #  there's no such thing yet, so here's American imperialism: en_US.UTF-8
    #  See: https://sourceware.org/glibc/wiki/Proposals/C.UTF-8)
    unset LANGUAGE LANG LC_COLLATE LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME
    export LC_ALL=en_US.UTF-8

    DEEPDIVE_BUNDLED_PREFIX="$DEEPDIVE_HOME"/lib/bundled/.all

    #  make sure DeepDive bundled executables are on the PATH
    PATH="$DEEPDIVE_BUNDLED_PREFIX/bin:$PATH"

    #  make sure bundled Python libraries and their entrypoint scripts are available
    PATH="$DEEPDIVE_HOME/lib/bundled/python-lib/prefix/bin:$PATH"
    python_version=$(python -V 2>&1 | sed 's/^Python \([0-9]*\.[0-9]*\)\..*/\1/')
    for python_lib_dir in "$DEEPDIVE_HOME"/lib/bundled/python-lib/prefix/lib/python"$python_version"/site-packages
    do  [[ -d "$python_lib_dir" ]] || continue
        PYTHONPATH="$python_lib_dir${PYTHONPATH:+:$PYTHONPATH}"
    done

    #  make sure ddlib in Python is available
    PYTHONPATH="$DEEPDIVE_HOME"/lib/python"${PYTHONPATH:+:$PYTHONPATH}"
    export PYTHONPATH

    #  make sure bundled Perl modules are available via local::lib
    set +u  # as perl local::lib can dereference unset variables such as PERL_LOCAL_LIB_ROOT
    eval $(perl -I"$DEEPDIVE_BUNDLED_PREFIX"/lib/perl5 -Mlocal::lib="$DEEPDIVE_BUNDLED_PREFIX" 2>/dev/null || true)
    set -u

    #  make sure DeepDive utilities are at the beginning of the PATH
    PATH="$DEEPDIVE_HOME/util:$DEEPDIVE_HOME/bin:$PATH"
    export PATH

    #  make sure bash is used as the default shell, e.g., by system(3), by make(1)
    export SHELL=$(type -p bash)

    # if we're running inside a Jupyter Notebook, tweak some configs to play nice with it
    if [[ -n ${JPY_PARENT_PID:-} ]]; then
        export DEEPDIVE_INTERACTIVE=false DEEPDIVE_SHOW_PROGRESS=false
    fi

    # turn on progress display only if we're running directly from a tty
    : ${DEEPDIVE_SHOW_PROGRESS:=$(if [[ -t 2 ]]; then echo true; else echo false; fi)}
    export DEEPDIVE_SHOW_PROGRESS

    export DEEPDIVE_SHELL=$0
fi


# Process common options
while getopts "C:" o; do
    case $o in
        C) export DEEPDIVE_APP=$OPTARG ;;
    esac
done
shift $(($OPTIND - 1))

# show usage with deepdive version at the first line
usage_with_version() {
    . "$DEEPDIVE_HOME"/.build-info.sh
    echo "# DeepDive ($version)"
    usage "$0" "$@" | tail -n +2
    [[ $# -eq 0 ]]
}

# Process input arguments
[ $# -gt 0 ] || {
    usage_with_version "No COMMAND given" || true
    echo
    deepdive-help | sed -n '/^# Available COMMANDs/,$p'
    false
}
Cmd=$1; shift


# Check if it's a valid command
exe=deepdive-"$Cmd"
if type "$exe" &>/dev/null; then
    set -- "$exe" "$@"
else
    usage_with_version "$Cmd: invalid COMMAND"
fi


# Run given command under this environment
exec "$@"
